/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.config.Config;
/*     */ import com.newcrm.exception.DBException;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class CustomerCategoryManager
/*     */ {
/*  23 */   private static Logger logger = Logger.getLogger(CustomerCategoryManager.class.getName());
/*     */ 
/*     */   public void init(int companyId, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/*  35 */     Config config = Config.getInstance();
/*  36 */     String customerCategory = config.customerCategory;
/*     */ 
/*  38 */     if ((customerCategory == null) || (customerCategory.trim().length() == 0)) {
/*  39 */       return;
/*     */     }
/*     */ 
/*  42 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  43 */     if (conn == null) {
/*  44 */       throw new DBException();
/*     */     }
/*  46 */     QueryRunner runner = new QueryRunner();
/*     */ 
/*  48 */     String[] arrCate = customerCategory.split(",");
/*     */     try {
/*  50 */       List baseParams = new ArrayList();
/*     */ 
/*  52 */       for (String cate : arrCate)
/*  53 */         if ((cate != null) && (cate.trim().length() != 0))
/*     */         {
/*  57 */           List info = new ArrayList();
/*  58 */           info.add(cate);
/*  59 */           info.add(Integer.valueOf(1));
/*  60 */           info.add(Integer.valueOf(companyId));
/*  61 */           info.add(userId);
/*  62 */           baseParams.add(info.toArray(new Object[0]));
/*     */         }
/*  64 */       runner.batch(conn, "INSERT INTO T_Customer_Category (CategoryName, CategoryType, CompanyId, CreateDate, CreateUserId, OrderNum) values (?, ?, ?, NOW(), ?, 0)", 
/*  65 */         (Object[][])baseParams.toArray(new Object[0][]));
/*     */     } finally {
/*     */       try {
/*  68 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  70 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> list(int companyId, int categoryType)
/*     */     throws DBException, SQLException
/*     */   {
/*  86 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  87 */     if (conn == null) {
/*  88 */       throw new DBException();
/*     */     }
/*     */ 
/*  91 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  93 */       return (List)runner.query(conn, "SELECT CategoryId, CategoryName,Color FROM T_Customer_Category WHERE CompanyId = ? AND CategoryType = ? ORDER BY OrderNum ASC", 
/*  94 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId), Integer.valueOf(categoryType) });
/*     */     } finally {
/*     */       try {
/*  97 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  99 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int add(int companyId, String name, int categoryType, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/* 117 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 118 */     if (conn == null) {
/* 119 */       throw new DBException();
/*     */     }
/* 121 */     GenKeyQueryRunner runner = new GenKeyQueryRunner(
/* 122 */       new ScalarHandler(), new String[] { "CategoryId" });
/*     */     try {
/* 124 */       runner.update(conn, "INSERT INTO T_Customer_Category (CategoryName, CategoryType, CompanyId, CreateDate, CreateUserId, OrderNum) values (?, ?, ?, NOW(), ?, 0)", new Object[] { name, 
/* 125 */         Integer.valueOf(categoryType), Integer.valueOf(companyId), userId });
/* 126 */       return ((Long)runner.getGeneratedKeys()).intValue();
/*     */     } finally {
/*     */       try {
/* 129 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 131 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int update(int categoryId, String name, String color, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/* 149 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 150 */     if (conn == null) {
/* 151 */       throw new DBException();
/*     */     }
/* 153 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/*     */       int i;
/* 156 */       if ((name == null) || (name.trim().length() == 0)) {
/* 157 */         return runner.update(conn, 
/* 158 */           "UPDATE T_Customer_Category SET Color = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE CategoryId = ?", new Object[] { color, userId, 
/* 159 */           Integer.valueOf(categoryId) });
/*     */       }
/* 161 */       return runner.update(conn, "UPDATE T_Customer_Category SET CategoryName = ? , Color = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE CategoryId = ?", new Object[] { 
/* 162 */         name, color, userId, Integer.valueOf(categoryId) });
/*     */     } finally {
/*     */       try {
/* 165 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 167 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int delete(int categoryId)
/*     */     throws DBException, SQLException
/*     */   {
/* 181 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 182 */     if (conn == null) {
/* 183 */       throw new DBException();
/*     */     }
/* 185 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 188 */       return runner.update(conn, "DELETE FROM T_Customer_Category WHERE CategoryId = ?", 
/* 189 */         Integer.valueOf(categoryId));
/*     */     } finally {
/*     */       try {
/* 192 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 194 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int updateSort(int categoryId, int order, String userId)
/*     */   {
/* 208 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 209 */     if (conn == null) {
/* 210 */       return 0;
/*     */     }
/* 212 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 215 */       return runner.update(conn, "UPDATE T_Customer_Category SET OrderNum = ? , UpdateDate = NOW(), UpdateUserId = ? WHERE CategoryId = ?", new Object[] { 
/* 216 */         Integer.valueOf(order), userId, Integer.valueOf(categoryId) });
/*     */     } catch (SQLException e) {
/* 218 */       logger.warn("update order failed", e);
/*     */     } finally {
/*     */       try {
/* 221 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 223 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 226 */     return 0;
/*     */   }
/*     */ 
/*     */   public long getTotalNum(Connection conn, int companyId)
/*     */   {
/* 237 */     if (conn == null) {
/* 238 */       return 0L;
/*     */     }
/* 240 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 243 */       Object obj = runner
/* 244 */         .query(conn, 
/* 245 */         "SELECT COUNT(*) FROM T_Customer_Category WHERE CompanyId = ? ", 
/* 246 */         new ScalarHandler(1), new Object[] { Integer.valueOf(companyId) });
/* 247 */       if (!(obj instanceof Long)) {
/* 248 */         return 0L;
/*     */       }
/* 250 */       return ((Long)obj).intValue();
/*     */     } catch (SQLException e) {
/* 252 */       logger.warn("get customer category total num failed", e);
/*     */     }
/* 254 */     return 0L;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.CustomerCategoryManager
 * JD-Core Version:    0.6.1
 */